package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.asktgapp.JGTool.EmojiFilter;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.application.MyApplication;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.dialog.JgLowDialog;
import com.asktgapp.eventbus.PayFinishEvent;
import com.asktgapp.hxsg.BiuEaseHelper;
import com.asktgapp.model.FreeAskSearchVO;
import com.asktgapp.model.LawVO;
import com.asktgapp.model.UserInfoVO;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.user.activity.PayActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.asktgapp.widget.LabelLayout;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 2018年8月9日 16:58:39
 * Description: 上门服务报价页面
 */

public class ServicePriceUpdateFragment extends BaseFragment {

    @InjectView(R.id.myBack)
    TextView myBack;
    @InjectView(R.id.myToolbarRight)
    TextView myToolbarRight;
    @InjectView(R.id.userName)
    TextView userName;

    @InjectView(R.id.payNumber)
    EditText payNumber;
    @InjectView(R.id.etPurposes)
    EditText etPurposes;
    @InjectView(R.id.commit)
    TextView commit;
    private EmojiFilter inputFilter;
    InputFilter[] inputFilters;
    private String id ;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_service_pirce_update, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {

        getBaseActivity().getToolbar().setVisibility(View.GONE);
        id = getBaseActivity().getIntent().getStringExtra("id");
        myBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBaseActivity().onBackPressed();
            }
        });
        inputFilter=new EmojiFilter();
        inputFilters=new InputFilter[]{inputFilter};
        etPurposes.setFilters(inputFilters);
        userName.setText(getBaseActivity().getIntent().getStringExtra("name"));
        myToolbarRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commitPay ();
            }
        });
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commitPay();
            }
        });

        String price = getActivity().getIntent().getStringExtra("price");
        String info = getActivity().getIntent().getStringExtra("info");
        if(!TextUtils.isEmpty(price)&&Double.valueOf(price)>0) {
            payNumber.setText(price);
        }
        if(!TextUtils.isEmpty(info))
            etPurposes.setText(info);
    }

    private void commitPay (){
        String money = payNumber.getText().toString();
        if(TextUtils.isEmpty(money)){
            showTost("请输入报价金额",0);
            return;
        }

        if(Double.valueOf(money.trim())<1){
            showTost("报价金额不能低于1元",0);
            return;
        }
        String other =  etPurposes.getText().toString();
        if(TextUtils.isEmpty(other)){
            showTost("请详细填写与用户协商的服务项目",0);
            return;
        }
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("userId", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        params.put("serverId", id);
        params.put("price", money);
        params.put("serverRange", other);
        params.put("page", 1);

        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.setServerPrice(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    showTost("提交成功",0);
                    getBaseActivity().onBackPressed();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }


    @Override
    public void loadData() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
